import { createStore } from 'vuex'

export default createStore({
  state: {
    isOpen: false,
    isLoading: false,
    keywords: '',
    items: JSON.parse(localStorage.getItem('readit-items')) || []
  },

  mutations: {
    setIsOpen(state, isOpen) {
      state.isOpen = isOpen
    },
    setIsLoading(state, isLoading) {
      state.isLoading = isLoading
    },
    setKeywords(state, keywords) {
      state.keywords = keywords
    },
    setItems(state, item) {
      state.items.push(item)
    }
  },

  getters: {
    filteredItems(state) {
      const reg = new RegExp(state.keywords, 'gi')
      return state.items.filter((value) => {
        return reg.test(value.title)
      })
    }
  },

  actions: {
  },
  
  modules: {
  }
})
